<?php
/**
 * FileModel.class.php
 * 文件管理模型
 * 
 * @author  王中艺
 * @date    2016-09-28
 */

namespace Admin\Model;
use Think\Model;

class FileModel extends Model {
    public $errCode = 1;
    public $errMsg  = null;

    /**
     * 创建文件夹
     * @param   $proId
     * @param   $parId
     * @param   $name
     * @param   $creater
     * @return  $boolean
     */
    function createFolder($proId, $parId, $name, $creater){
        // $info   = array(
        //     'pro_id'    => $proId,
        //     'par_id'    => $parId,
        //     'name'  => $name,
        //     'create_time'   => time(),
        //     'creater'  => $creater,
        //     'type'  => 1,
        //     'status'    => 1
        // );

        // $result     = $this->add($info);
        // if($result === false)
        //     $this->errCode  = 120;

        // return $this->errCode;
    }

    /**
     * 文件上传
     * @param   $proId
     * @param   $parId
     * @param   $name
     * @param   $uploader
     * @param   $path
     * @return  $uploader
     */
    function uploadFile($proId, $parId, $name, $uploader, $path){
        $info   = array(
            'pro_id'    => $proId,
            'par_id'    => $parId,
            'name'  => $name,
            'create_time'   => time(),
            'uploader'  => $uploader,
            'type'  => 2,
            'path'  => $path,
            'down_count'    => 0,
            'status'    => 1
        );

        $result     = $this->add($info);
        if($result === false)
            $this->errCode  = 120;

        return $this->errCode;
    }

    /**
     * 获取文件列表
     * @param   $proId
     * @param   $parId
     * @return  $list
     */
    function getList($proId = 0, $parId = 0){
        $list   = $this->where('pro_id = %d and par_id = %d and status != -1', $proId, $parId)
            ->order('type, id desc')
            ->select();

        if($list === false)
            $this->errCode  = 122;

        return $list;
    }

    /**
     * 删除文件
     * @param   $fileId
     * @return  $errCode
     */
    function delFile($fileId){
        $info   = array(
            'id'    => $fileId,
            'status'    => -1
        );

        $result     = $this->save($info);
        if($result === false)
            $this->errCode  = 124;

        return $this->errCode;
    }
}